<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>vue</title>
  <script src="./js/vue.js"></script>
</head>

<body>
  <div id="app">
    <div>
      <!-- 事件修饰符 -->
      <!-- <input type="text" :value="title" @input="inputFn" @keyup.ctrl.enter="onenter"> -->
      <input type="text" :value="title" @input="inputFn" @keyup.prevent.alt.f1="onenter">
    </div>
    <div>
      <ul>
        <li v-for="(item,index) in todos" :key="item.id">
          <span>{{item.title}}</span> --
          <span @click="delTodo(index)">删除</span>
        </li>
      </ul>
    </div>

  </div>

  <script>
    // 自定义按键修饰符别名
    // 字符串中通过charCodeAt得到的ascii码值
    Vue.config.keyCodes.f1 = 112

    new Vue({
      el: '#app',
      data: {
        title: '',
        todos: []
      },
      methods: {
        inputFn(evt) {
          this.title = evt.target.value.trim()
        },
        // 此事件只有回车才有触发
        onenter(evt) {
          // push它是vue重构一个数组对象的方法，调用它会触发视图更新
          this.todos.push({
            id: Date.now(),
            title: this.title
          })
          this.title = ''
        },
        delTodo(index) {
          this.todos.splice(index, 1)
        }
      }
    })
  </script>
</body>

</html>